﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using System.Collections;
using System.Web.Mvc;
using MvcLibrary.Repository;

namespace WebVideo.Areas.Admin.Models
{
    public class NewsModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Tiêu đề không được rỗng")]
        [StringLength(200, ErrorMessage = "Tiêu đề phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        public string Title { get; set; }

        [Required(ErrorMessage = "Nội dung không được rỗng")]
        public string Des { get; set; }

        public DateTime? Date { get; set; }

        public string Image { get; set; }
        public int IdTypeNews { get; set; }
        public string MetaDescription { get; set; }
        public string MetaKeyWord { get; set; }

        public IEnumerable<EntityTypeNew> DanhMuc
        {
            get
            {
                dbVideoDataContext db = new dbVideoDataContext();
                var query = from p in db.EntityTypeNews
                            select p;
                return query.ToList();
            }
            set
            {

            }
        }
        // load index
        public static IEnumerable<EntityNew > LayDuLieu()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityNews
                        orderby p.Date descending
                        select p;

            return query.ToList();
        }

        // insert
        public static void Insert(NewsModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();


            EntityNew tnew = new EntityNew();

            tnew.Title  = Mtnew.Title;
            tnew.Des = Mtnew.Des;
            tnew.Date = DateTime.Now;
            tnew.Image = Mtnew.Image;
            tnew.MetaDescription = Mtnew.MetaDescription;
            tnew.MetaKeyWord = Mtnew.MetaKeyWord;
            tnew.IdTypeNews = Mtnew.IdTypeNews;
            db.EntityNews.InsertOnSubmit(tnew);
            db.SubmitChanges();

        }

        public static NewsModel LayModel(int IdNews)
        {
            NewsModel Mtnew = new NewsModel();


            dbVideoDataContext db = new dbVideoDataContext();

            EntityNew  tnew = db.EntityNews.Where(p => p.Id == IdNews).Single();

            Mtnew.Id = tnew.Id;
            Mtnew.Title = tnew.Title;
            Mtnew.Des = tnew.Des;
           
            Mtnew.Image = tnew.Image;
            Mtnew.MetaDescription = tnew.MetaDescription;
            Mtnew.MetaKeyWord = tnew.MetaKeyWord;
            Mtnew.IdTypeNews = (int)tnew.IdTypeNews;
            return Mtnew;

        }
        public static string Edit(NewsModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityNew tnew = db.EntityNews.Where(p => p.Id == Mtnew.Id).Single();
            tnew.Id = Mtnew.Id;
            tnew.Title = Mtnew.Title;
            tnew.Des = Mtnew.Des;

            string image = null;
            if (Mtnew.Image != null)
            {
                image = tnew.Image;
                tnew.Image = Mtnew.Image;
            }
           
            tnew.MetaDescription = Mtnew.MetaDescription;
            tnew.MetaKeyWord = Mtnew.MetaKeyWord;
            tnew.IdTypeNews = Mtnew.IdTypeNews;
            db.SubmitChanges();
            return image;

        }

        public static string Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityNew tnew = db.EntityNews.Where(p => p.Id == Id).Single();
            
            db.EntityNews.DeleteOnSubmit(tnew);
            db.SubmitChanges();
            return tnew.Image;

        }


    }
}